package com.minesweeper.ui;

import com.minesweeper.data.GameData;
import com.minesweeper.data.ImagesData;
import com.minesweeper.tools.GameStatusListener;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

/**
 * @author skniy
 */
public class FunctionPanel extends JPanel {

    /**
     * 地雷数目个位
     */
    private JLabel mineCountUnit;
    /**
     * 地雷数目十位
     */
    private JLabel mineCountTen;
    /**
     * 地雷数目百位
     */
    private JLabel mineCountHundred;
    /**
     * 游戏状态
     */
    private JLabel gameStatus;
    /**
     * 时间个位
     */
    private JLabel timeCountUnit;
    /**
     * 时间十位
     */
    private JLabel timeCountTen;
    /**
     * 时间百位
     */
    private JLabel timeCountHundred;
    /**
     * 游戏界面
     */
    private GameInterface gameInterface;

    /**
     * 构造方法
     *
     * @param gameInterface
     */
    public FunctionPanel(GameInterface gameInterface) {
        //初始化组件
        this.gameInterface = gameInterface;
        this.mineCountUnit = new JLabel();
        this.mineCountTen = new JLabel();
        this.mineCountHundred = new JLabel();
        this.gameStatus = new JLabel();
        this.timeCountUnit = new JLabel();
        this.timeCountTen = new JLabel();
        this.timeCountHundred = new JLabel();
        //设置边界布局为水平布局
        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        //加入各组件
        addItems();
    }

    /**
     * 加入各组件
     */
    private void addItems() {
        //构造一个面板，这主要是为保持内部组件与边框的边距
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.LINE_AXIS));
        //按位设置地雷数目
        mineCountHundred.setIcon(ImagesData.ICON_NUMBER[GameData.MINE_COUNT / 100]);
        mineCountTen.setIcon(ImagesData.ICON_NUMBER[(GameData.MINE_COUNT / 10) % 10]);
        mineCountUnit.setIcon(ImagesData.ICON_NUMBER[GameData.MINE_COUNT % 10]);
        //设置游戏状态并加入鼠标监视器
        gameStatus.setIcon(ImagesData.ICON_GAME_STATUS_NOTHING);
        gameStatus.addMouseListener(new GameStatusListener(gameStatus, gameInterface));
        //按位设置时间
        timeCountHundred.setIcon(ImagesData.ICON_NUMBER[0]);
        timeCountTen.setIcon(ImagesData.ICON_NUMBER[0]);
        timeCountUnit.setIcon(ImagesData.ICON_NUMBER[0]);
        //加入左边距
        jPanel.add(Box.createHorizontalStrut(2));
        //加入地雷数目
        jPanel.add(mineCountHundred);
        jPanel.add(mineCountTen);
        jPanel.add(mineCountUnit);
        //加入左弹性条
        jPanel.add(Box.createHorizontalGlue());
        //加入游戏状态
        jPanel.add(gameStatus);
        //加入右弹性条
        jPanel.add(Box.createHorizontalGlue());
        //加入时间
        jPanel.add(timeCountHundred);
        jPanel.add(timeCountTen);
        jPanel.add(timeCountUnit);
        //加入右边距
        jPanel.add(Box.createHorizontalStrut(2));
        //设置面板边距
        Border borderIn = BorderFactory.createLoweredBevelBorder();
        Border borderOut = BorderFactory.createEmptyBorder(2, 2, 2, 2);
        Border borderCom = BorderFactory.createCompoundBorder(borderIn, borderOut);
        jPanel.setBorder(borderCom);
        jPanel.setBackground(Color.LIGHT_GRAY);
        //加入新建的面板
        this.add(jPanel);
        //设置此面板的外边距
        borderOut = BorderFactory.createEmptyBorder(5, 5, 5, 5);
        this.setBorder(borderOut);
        this.setBackground(Color.LIGHT_GRAY);
    }

    /**
     * 设定时间
     *
     * @param time
     */
    public void setTime(int time) {
        timeCountUnit.setIcon(ImagesData.ICON_NUMBER[time % 10]);
        timeCountTen.setIcon(ImagesData.ICON_NUMBER[time / 10 % 10]);
        timeCountHundred.setIcon(ImagesData.ICON_NUMBER[time / 100]);
    }


    /**
     * 设定地雷数目
     *
     * @param mineCount
     */
    public void setMineCount(int mineCount) {
        int unit, ten, hundred;
        if (mineCount >= 0) {
            unit = mineCount % 10;
            ten = mineCount / 10 % 10;
            hundred = mineCount / 100;
        } else {
            unit = -mineCount % 10;
            ten = -mineCount / 10 % 10;
            hundred = 10;
        }
        mineCountUnit.setIcon(ImagesData.ICON_NUMBER[unit]);
        mineCountTen.setIcon(ImagesData.ICON_NUMBER[ten]);
        mineCountHundred.setIcon(ImagesData.ICON_NUMBER[hundred]);
    }

    public JLabel getGameStatus() {
        return gameStatus;
    }

    public JLabel getMineCountUnit() {
        return mineCountUnit;
    }

    public JLabel getMineCountHundred() {
        return mineCountHundred;
    }

    public JLabel getMineCountTen() {
        return mineCountTen;
    }

    public JLabel getTimeCountUnit() {
        return timeCountUnit;
    }

    public JLabel getTimeCountTen() {
        return timeCountTen;
    }

    public JLabel getTimeCountHundred() {
        return timeCountHundred;
    }
}
